package demo1;

import java.util.Arrays;
import java.util.Objects;

class  Student implements  Comparable<Student> {
    public String name;
    public  int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o)
    {
        return this.name.compareTo(o.name);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
public class Test_1 {
    public  static  void MySort(Comparable[] comparables){
        for (int i = 0; i <comparables.length - 1 ; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if (comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("222",3333);
        Student student2 = new Student("222",3333);
        System.out.println(student1.equals(student2));

    }

    public static void main3(String[] args) {
        Student []students =new Student[3];
        students [0] = new Student("imok",22);
        students [1] = new Student("susan",17);
        students [2] = new Student("sula",69);
        System.out.println("交换前："+Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println("交换后："+Arrays.toString(students));
    }
    public static void main2(String[] args) {
        Student []students =new Student[3];
        students [0] = new Student("imok",22);
        students [1] = new Student("susan",17);
        students [2] = new Student("sula",69);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println("交换前："+Arrays.toString(students));
        Arrays.sort(students,ageComparator);
        System.out.println("交换后："+Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student []students =new Student[3];
        students [0] = new Student("imok",22);
        students [1] = new Student("susan",17);
        students [2] = new Student("sula",69);
        System.out.println("交换前；"+Arrays.toString(students));
       MySort(students);
        System.out.println("交换后；"+Arrays.toString(students));
    }
    public static void main0(String[] args) {
        Student []students =new Student[3];
         students [0] = new Student("susan",17);
         students [1] = new Student("imok",22);
        System.out.println(students[0].compareTo(students[1]));
    }
}
